<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;
use think\session;
// 应用公共文件

/**
 * 全局变量
 *
 * @param $name 变量名
 * @param string $value 变量值
 * @return mixed 返回值
 */
if (!function_exists('v')) {
    function v($name = null, $value = '[null]')
    {
        static $vars = [];
        if (is_null($name)) {
            return $vars;
        } else if ($value == '[null]') {
            //取变量
            $tmp = $vars;
            foreach (explode('.', $name) as $d) {
                if (isset($tmp[$d])) {
                    $tmp = $tmp[$d];
                } else {
                    return null;
                }
            }
            return $tmp;
        } else {
            //设置
            $tmp = &$vars;
            foreach (explode('.', $name) as $d) {
                if (!isset($tmp[$d])) {
                    $tmp[$d] = [];
                }
                $tmp = &$tmp[$d];
            }
            return $tmp = $value;
        }
    }
}

/**
 * 递归输出
 * @title  后台左侧列表栏目显示
 * @param $items 数组
 * @param string $id
 * @param string $pid 父栏目id
 * @param string $son 子栏目数组
 * @return array
 */
function getMenu($items, $id = 'id', $pid = 'pid', $son = 'children')
{
    $tree = array();
    $tmpMap = array();

    foreach ($items as $item) {
        $tmpMap[$item[$id]] = $item;
    }

    foreach ($items as $item) {
        if (isset($tmpMap[$item[$pid]])) {
            $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
        } else {
            $tree[] = &$tmpMap[$item[$id]];
        }
    }
    return $tree;
}

/**
 * 检测用户权限
 * @param array $menus 权限列表
 * @param string $url 检测的权限
 * @return boolean
 */
function has_menu ($menus,$url = null){
    $nowUrl = $url ? $url : ucfirst(\think\Request::instance()->controller()).'/'. strtolower(\think\Request::instance()->action());
    $allowList = array('index/index','index/profile','index/upload','index/trash','index/ueditor');
    if(in_array($nowUrl,$allowList)) return true;
    $result = false;
    foreach($menus as $k=>$v){
        if($v['url'] == $nowUrl){
            $result = true;
            break;
        }else{
            if(isset($v['children'])) {
                foreach ($v['children'] as $kk => $vv) {
                    if ($vv['url'] == $nowUrl) {
                        $result = true;
                        break;
                    }else{
                        if(isset($vv['children'])) {
                            foreach ($vv['children'] as $kkk => $vvv) {
                                if ($vvv['url'] == $nowUrl) {
                                    $result = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $result;
}

/**
 * 后台操作日志
 * @param array $right 拥有可操作的列表
 */
function action_logs ($right){
    $menus = ucfirst(\think\Request::instance()->controller()).'/'. strtolower(\think\Request::instance()->action());
    $param = \think\Request::instance()->isPost() ? input('post.') : input('get.');
    $param['type'] = $_SERVER['REQUEST_METHOD'];
    $data = array(
        'admin_id' => Session::get('adminid'),
        'username' => Session::get('account'),
        'action' => get_menu($right,$menus),
        'url' => $_SERVER['REQUEST_URI'],
        'param' => json_encode($param),
        'add_time' => time()
    );
    DB::name('admin_logs')->insert($data);
}

/**
 * 获取操作名称
 * @param array $right 拥有可操作的列表
 * @param string $url 操作的url
 */
function get_menu ($right,$url){
    $str = '';
    $allowList = array('index/index','index/profile','index/upload','index/trash','index/ueditor');
    foreach($right as $k => $v){
        if($v['url'] == $url){
            $str = $v['title'];
            break;
        }else{
            if(isset($v['children'])) {
                foreach ($v['children'] as $kk => $vv) {
                    if ($vv['url'] == $url) {
                        $str = $vv['title'];
                        break;
                    }else{
                        if(isset($vv['children'])) {
                            foreach ($vv['children'] as $kkk => $vvv) {
                                if ($vvv['url'] == $url) {
                                    $str = $vvv['title'];
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $str;
}

//记录发货和开通会员成功后发送短信的信息
function rwLog(&$phone=null,&$status=null){
    $filename = "./logs/".date("Y-m-d").".log";
    if(file_exists($filename)){
        /*数组写入*/
        $arr = array('phone'=>$phone,'status'=>$status,'time'=>date("Y-m-d H:i:s"));
        file_put_contents($filename, print_r ($arr,true),FILE_APPEND);/*FILE_APPEND:追加文件写入*/
    }else{
        fopen($filename, "w");/*创建log文件*/
        $arr = array('phone'=>$phone,'status'=>$status,'time'=>date("Y-m-d H:i:s"));
        file_put_contents($filename, print_r ($arr,true),FILE_APPEND);/*FILE_APPEND:追加文件写入*/
    }
}

/**
 * 获得用户的积分
 */
function integral($uid=null,$type=null){
    $bef_integral = (new \app\index\model\IndentIntegral())->where(["user_id"=>$uid,'type'=>$type])->sum('integral');
    $for_integral = (new \app\index\model\IntegralUsed())->where(["iu_uid"=>$uid,'iu_type'=>$type,'iu_status'=>1])->sum('iu_integral');
    $integral = $bef_integral-$for_integral;
    return $integral;
}
